package com.zivwu.utils.device;


import android.Manifest;
import android.app.Application;
import android.os.Environment;

import com.zivwu.utils.AppUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 1. file  路径提供者
 * 2. file  文件存取
 * 3. APP   空间检查
 */
public class FileUtils {
    public static int MIN_SPACE = 100;


    /**
     * 外置存储，读写权限
     */
    private static String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE};


    /**
     * get Android Cache File
     *
     * @return if sd card && hasPermission @permissions return context.getExternalCacheDir()
     * else return context.getCacheDir()
     */
    public static File provideCacheFile() {
        Application app = AppUtils.getApp();
        File storageDir = app.getExternalCacheDir();
        if (!PermissionHelper.hasPermission(permissions)
                || !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || storageDir == null) {
            storageDir = app.getCacheDir();
        }
        return storageDir;
    }


    public static File provideDiskFile() {
        File file = provideCacheFile();
        File disk = new File(file, "disk");
        if (!disk.exists())
            disk.mkdirs();
        return disk;
    }

    public static File provideImageFile() {
        File file = provideCacheFile();
        File disk = new File(file, "images");
        if (!disk.exists())
            disk.mkdirs();
        return disk;
    }


    public static File provideImageStampFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File file = provideCacheFile();
        if (file == null)
            return null;
        String IMAGE_DIR = "images";
        return provideImageCacheFile(file, IMAGE_DIR, imageFileName);

    }


    public static File provideImageCacheFile(File dir, String child, String name) throws IOException {
        File image;
        File imageFile = new File(dir, child);
        if (!imageFile.exists()) {
            boolean mkdirs = imageFile.mkdirs();
            if (!mkdirs)
                return null;
        }
        image = File.createTempFile(
                name,  /* prefix */
                ".jpg",         /* suffix */
                imageFile      /* directory */
        );

        return image;
    }


    /**
     * check file how has low space
     *
     * @param file the file need to write
     * @return has space true else false
     */
    public static boolean isEnoughSpace(File file) {
        return isEnoughSpace(file, MIN_SPACE);
    }

    /**
     * check file how has low space
     *
     * @param file the file need to write
     * @param size need how match space MB
     * @return has space true else false
     */
    public static boolean isEnoughSpace(File file, long size) {
        long space = file.getUsableSpace() >> 20;
        return space >= size;

    }


}
